package com.riversoft.weixin.app.wxacode;

import com.riversoft.weixin.app.AppWxClientFactory;
import com.riversoft.weixin.app.base.AppSetting;
import com.riversoft.weixin.app.base.WxEndpoint;
import com.riversoft.weixin.common.WxClient;
import com.riversoft.weixin.common.util.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

public class WxaCode{
    private static Logger logger = LoggerFactory.getLogger(WxaCode.class);

    private WxClient wxClient;

    public static WxaCode defaultWxaCode() {
        return with(AppSetting.defaultSettings());
    }

    public static WxaCode with(AppSetting appSetting) {
        WxaCode wxaCode = new WxaCode();
        wxaCode.setWxClient(AppWxClientFactory.getInstance().with(appSetting));
        return wxaCode;
    }
    public void setWxClient(WxClient wxClient) {
        this.wxClient = wxClient;
    }


    public InputStream createWxaCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline){
        WxMaWxcode wxMaWxcode = new WxMaWxcode();
        wxMaWxcode.setPath(path);
        wxMaWxcode.setWidth(width);
        wxMaWxcode.setAutoColor(autoColor);
        wxMaWxcode.setLineColor(lineColor);
        wxMaWxcode.setHyaline(isHyaline);
        String url = WxEndpoint.get("url.wxa.wxacode");
        return wxClient.copyStream(url,JsonMapper.defaultMapper().toJson(wxMaWxcode));
    }

    public InputStream createWxaCode(String path, int width) {
        return createWxaCode(path, width, true, (WxMaCodeLineColor)null, false);
    }

    public InputStream createWxaCode(String path){
        return createWxaCode(path, 430, true, (WxMaCodeLineColor)null, false);
    }

    public InputStream createWxaCodeUnlimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline){
        WxaCodeUnlimit wxaCodeUnlimit = new WxaCodeUnlimit();
        wxaCodeUnlimit.setScene(scene);
        wxaCodeUnlimit.setPage(page);
        wxaCodeUnlimit.setWidth(width);
        wxaCodeUnlimit.setAutoColor(autoColor);
        wxaCodeUnlimit.setLineColor(lineColor);
        wxaCodeUnlimit.setHyaline(isHyaline);
        String url = WxEndpoint.get("url.wxa.wxacodeunlimit");
        return wxClient.copyStream(url,JsonMapper.defaultMapper().toJson(wxaCodeUnlimit));
    }

    public InputStream createWxaCodeUnlimit(String scene, String page){
        return createWxaCodeUnlimit(scene, page, 430, true, (WxMaCodeLineColor)null, false);
    }
}
